

import 'package:json_annotation/json_annotation.dart';
import 'package:mtf/app/net/EntityFactory.dart';

@JsonSerializable(genericArgumentFactories: true)
class DioResponse<T> {

  /// 消息(例如成功消息文字/错误消息文字)
  final String? msg;
  /// 自定义code(可根据内部定义方式)
  final int? code;
  /// 接口返回的数据
  final T? data;
  /// 需要添加更多
  /// .........

  DioResponse({
    this.msg,
    this.data,
    this.code,
  });


  @override
  String toString() {
    StringBuffer sb = StringBuffer('{');
    sb.write("\"msg\":\"$msg\"");
    sb.write(",\"code\":\"$code\"");
    sb.write(",\"data\":\"$data\"");
    sb.write('}');
    return sb.toString();
  }

  bool isSuccess(){
    return code==200;
  }

  bool isSuccessData(){
    return code==200&&data!=null;
  }


  factory DioResponse.fromJson(Map<String, dynamic> json) => DioResponse(
      code: json["code"],
      msg: json["msg"],
      data: EntityFactory.generateOBJ<T>(json['data'])
  );


  Map<String, dynamic> toJson() => {
    "code": code,
    "msg": msg,
    "data": data,
  };
}

class DioResponseCode {
  /// 成功
  static const int SUCCESS = 0;
  /// 错误
  static const int ERROR = 1;
/// 更多
}